package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.edu.domain.vo.CoursePublishVo;
import com.ruoyi.edu.service.IEduChapterService;
import com.ruoyi.edu.service.IEduVideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edu.domain.EduCourse;
import com.ruoyi.edu.service.IEduCourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author niruichen
 * @date 2020-12-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/edu/course" )
public class EduCourseController extends BaseController {

    private final IEduCourseService iEduCourseService;

    @Autowired
    private IEduChapterService chapterService;

    @Autowired
    private IEduVideoService videoService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('edu:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduCourse eduCourse) {
        startPage();
        List<EduCourse> list = iEduCourseService.queryList(eduCourse);
        return getDataTable(list);
    }

    /**
     * 获取课程发布信息
     */
    @PreAuthorize("@ss.hasPermi('edu:course:getPublish')" )
    @GetMapping("/getPublish/{id}" )
    public AjaxResult getPublish(@PathVariable("id") String id) {
        CoursePublishVo coursePublishVo = iEduCourseService.publishCourseInfo(id);
        return AjaxResult.success(coursePublishVo);
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('edu:course:publish')" )
    @PostMapping("publish/{id}")
    public AjaxResult publish(@PathVariable("id") String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        return toAjax(iEduCourseService.updateById(eduCourse) ? 1 : 0);
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('edu:course:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(iEduCourseService.getById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('edu:course:add')" )
    @Log(title = "课程" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduCourse eduCourse) {
        boolean save = iEduCourseService.save(eduCourse);
        if(save){
            return AjaxResult.success(eduCourse.getId());
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('edu:course:edit')" )
    @Log(title = "课程" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduCourse eduCourse) {
        return toAjax(iEduCourseService.updateById(eduCourse) ? 1 : 0);
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('edu:course:remove')" )
    @Log(title = "课程" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable String id) {
        //删除小节
        videoService.removeByCourseId(id);
        //删除章节
        chapterService.removeByCourseId(id);
        //删除课程
        return toAjax(iEduCourseService.removeById(id) ? 1 : 0);
    }
}
